﻿using FieldTools.Client.Command;
using FieldTools.Client.Model.Window;
using FieldTools.Util;
using FieldTools.Util.Helper;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FieldTools.Client.ViewModel.Window
{
    /// <summary>
    /// 进程管理VM
    /// </summary>
    public class ProcessViewModel : NotificationObject, ISingletonDependency
    {
        /// <summary>
        /// 进程
        /// </summary>
        public ProcessViewModel()
        {
            Console.WriteLine("进入进程管理工具");
            //待传入取消标记todo
            Task.Run(async () => {
            while (true)
            {
                await Task.Delay(2000);
                GetProcessF();
            }
            });
        }

        #region 实体
        ProcessModel obj = new();
        /// <summary>
        /// 进程
        /// </summary>
        public Process? Process
        {
            get { return obj.Process; }
            set { obj.Process = value; this.RaisePropertyChanged(nameof(Process)); }
        }

        /// <summary>
        /// 进程
        /// </summary>
        public List<ProcessThread>? ProcessThreads
        {
            get { return obj.ProcessThreads; }
            set { obj.ProcessThreads = value; this.RaisePropertyChanged(nameof(ProcessThreads)); }
        }

        private List<TitleContent> processContents;
        /// <summary>
        /// 进程字段展示数据
        /// </summary>
        public List<TitleContent> ProcessContents
        {
            get { return processContents; }
            set { processContents = value; this.RaisePropertyChanged(nameof(ProcessContents)); }
        }

        /// <summary>
        /// 是否自动刷新
        /// </summary>
        public bool? AutoRefreshStatus
        {
            get { return obj.AutoRefreshStatus; }
            set { obj.AutoRefreshStatus = value; this.RaisePropertyChanged(nameof(AutoRefreshStatus)); }
        }

        /// <summary>
        /// 自动刷新时间
        /// </summary>
        public int? RefreshTimespan
        {
            get { return obj.RefreshTimespan; }
            set { obj.RefreshTimespan = value; this.RaisePropertyChanged(nameof(RefreshTimespan)); }
        }

        #endregion

        #region 事件
        private BaseCommand getProcess;
        /// <summary>
        /// 读取进程
        /// </summary>
        public BaseCommand GetProcess
        {
            get
            {
                if (getProcess == null)
                {
                    getProcess = new BaseCommand(new Action<object>(o =>
                    {
                        GetProcessF();
                    }));
                }
                return getProcess;
            }
        }
        #endregion

        #region 方法
        private void GetProcessF()
        {
            Console.WriteLine(DateTime.Now.ToLongTimeString()+"：刷新获取进程信息");
            using var process = ProcessHelper.GetTheProcess();
            if (process != null)
            {
                List<TitleContent> titleContents = new()
                {
                    new(){Title="唯一标识符", Content=process.Id.ToString()},
                    new(){Title="主窗口标题", Content=process.MainWindowTitle.ToString()},
                    new(){Title="进程名", Content=process.ProcessName.ToString()},
                    new(){Title="本机句柄", Content=process.Handle.ToString()},
                    new(){Title="终端服务会话标识符", Content=process.SessionId.ToString()},
                    new(){Title="启动时间", Content=process.StartTime.ToString("G")},
                    new(){Title="处理器时间", Content=process.TotalProcessorTime.TotalSeconds+" s"},
                    new(){Title="用户处理器时间", Content=process.UserProcessorTime.TotalSeconds+" s"},
                    new(){Title="主窗口的窗口句柄", Content=process.MainWindowHandle.ToString()},
                    new(){Title="基本优先级", Content=process.BasePriority.ToString()},
                    new(){Title="分配的非分页系统内存量", Content=process.NonpagedSystemMemorySize64.ToString()+" byte"},
                    new(){Title="分配的分页内存量", Content=process.PagedMemorySize64.ToString()+" byte"},
                    new(){Title="可分页系统内存量", Content=process.PagedSystemMemorySize64.ToString()+" byte"},
                    new(){Title="虚拟内存分页文件中的最大内存量", Content=process.PeakPagedMemorySize64.ToString()+" byte"},
                    new(){Title="使用的最大虚拟内存量", Content=process.PeakVirtualMemorySize64.ToString()+" byte"},
                    new(){Title="使用的最大物理内存量", Content=process.PeakWorkingSet64.ToString()+" byte"},
                    new(){Title="专用内存量", Content=process.PrivateMemorySize64.ToString()+" byte"},
                    new(){Title="虚拟内存量", Content=process.VirtualMemorySize64.ToString()+" byte"},
                    new(){Title="物理内存量", Content=process.WorkingSet64.ToString()+" byte"},
                };
                ProcessContents = titleContents;

                Process = process;
                List<ProcessThread> processThreads = new();
                foreach (var thread in process.Threads)
                {
                    processThreads.Add((ProcessThread)thread);
                }
                if (processThreads.Any())
                {
                    ProcessThreads = processThreads;
                }
            }
        }
        #endregion
    }
}
